/*
 *  $Id: Consumption.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Water.Spells {
    public class Consumption: SpellCard {
        public Consumption(): base("001_w_s_consumption") {
            Level = CompleteLevel.ReadyForTest;
            Version = 1;
            Element = ElementType.Water;
            Value = 100;
            AddManaCost(ElementType.Water, 1);
        }

        private static readonly Languages.CardPhrase<Consumption> NoTargets = new Languages.CardPhrase<Consumption>("no_targets");

        public override bool IsPossibleAction(TargetArgs args) {
            List<Creature> victims = new List<Creature>(GetVictims(args.Player));
            if (victims.Count == 0) {
                args.Player.Game.ErrorNotice(NoTargets.Phrase.GetText());
                return false;
            }
            int mana = 1 + 2*victims.Count;
            if (mana >= args.Player.GetMana(ElementType.Water)) {
                args.Player.Game.ErrorNotice(ManaCondition.NotEnough.GetText(ElementType.Water));
                return false;
            }
            return true;
        }

        public override void DoAction(TargetArgs args) {
            int health = 0, mana = 0;
            foreach (Creature cr in GetVictims(args.Player)) {
                health += cr.Health;
                mana += 2;
                cr.InstantDeath();
            }
            args.Player.DecrementMana(ElementType.Water, mana);
            args.Player.HealBy(NewDamage(args, DamageType.Regeneration, health), args.Player.MaxHealth*5/4);
        }

        private static IEnumerable<Creature> GetVictims(Player owner) {
            foreach (Creature cr in owner.GetCreatures())
                if (cr.Card.Element == ElementType.Water && !cr.HasFlag<Affects.Antimagic>())
                    yield return cr;
        }
    }
}
